package example.com.zcq.mymj.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ajguan.library.EasyRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import example.com.zcq.mymj.Adapter.JiazhaoAdapter;
import example.com.zcq.mymj.R;
import example.com.zcq.mymj.bean.JiaoZhao;
import example.com.zcq.mymj.net.HttpInterface;
import example.com.zcq.mymj.net.RetrofitHttp;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author zcq
 * @package example.com.zcq.mymj.fragment
 * @fileName MyMj
 * @description: 这个类的描述
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/11/22  19:59
 */
public class Fragment2 extends Fragment {

    private View view;
    /**
     * 每日一看
     */
    private TextView mTxtTopbar;
    private RelativeLayout mLyTopBar;
    private RecyclerView mRc2;
    private List<JiaoZhao.ResultBean> mResultBeanList = new ArrayList<>();
    private JiazhaoAdapter jiazhaoAdapter;
    private EasyRefreshLayout mNewsRefreshLayout;
    private HashMap<String, String> hashMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg2, container, false);


        initView(view);
        initData();
        return view;
    }

    private void initData() {
         hashMap = new HashMap<>();
        hashMap.put("key", "b3e1dc9836585b4e4323cc5b924b5c60");
        hashMap.put("subject", "1");
        hashMap.put("model", "c1");
        hashMap.put("testType", "rand");


        HttpInterface h = RetrofitHttp.getSimpleRetrofit();

        h.jiaokaotiku(hashMap).enqueue(new Callback<JiaoZhao>() {
            @Override
            public void onResponse(Call<JiaoZhao> call, Response<JiaoZhao> response) {
                JiaoZhao data = response.body();
                if (data != null) {
                    mResultBeanList.clear();
                    mResultBeanList.addAll(data.getResult());
                    jiazhaoAdapter.notifyDataSetChanged();

                }
                if (mNewsRefreshLayout.isLoading() || mNewsRefreshLayout.isRefreshing()) {
                    mNewsRefreshLayout.loadMoreComplete();

                }

            }

            @Override
            public void onFailure(Call<JiaoZhao> call, Throwable t) {
                if (mNewsRefreshLayout.isLoading() || mNewsRefreshLayout.isRefreshing()) {
                    mNewsRefreshLayout.loadMoreFail();

                }

            }
        });
    }

    private void initView(View view) {
        mTxtTopbar = (TextView) view.findViewById(R.id.txt_topbar);
        mLyTopBar = (RelativeLayout) view.findViewById(R.id.ly_top_bar);
        mRc2 = (RecyclerView) view.findViewById(R.id.rc_2);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        mRc2.setLayoutManager(staggeredGridLayoutManager);
        jiazhaoAdapter = new JiazhaoAdapter(getActivity(), mResultBeanList);
        mRc2.setAdapter(jiazhaoAdapter);
        mNewsRefreshLayout = (EasyRefreshLayout) view.findViewById(R.id.news_refresh_layout);

        mNewsRefreshLayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            @Override
            public void onLoadMore() {

                /**
                 * 实现加载更多数据的操作
                 **/
                mNewsRefreshLayout.loadMoreComplete();   // 加载更多完成后实现的操作(动画效果)
                hashMap.put("testType", "order");
                initData();
            }

            @Override
            public void onRefreshing() {

                /**
                 * 实现下拉刷新数据的操作
                 **/
                mNewsRefreshLayout.refreshComplete();
                // 下拉刷新完成后实现的操作(动画效果)
                hashMap.put("testType", "rand");

                initData();
            }
        });
    }
}
